package com.example.lt.controller;

import com.example.lt.constant.MessageConstant;
import com.example.lt.constant.PicPathConstant;
import com.example.lt.pojo.entity.SongList;
import com.example.lt.pojo.vo.SongListVO;
import com.example.lt.result.Result;
import com.example.lt.service.SongListService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.session.InMemoryWebSessionStore;

import java.util.List;

/**
 * @Author 小涛Tao
 * @Date: 2024/05/31/13:35
 * version 1.0 注释：
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/songList")
public class SongListController {

    private final SongListService songListService;

    /**
     * 管理端添加歌单
     * @param songList
     * @return
     */
    @PostMapping("/admin/add")
    public Result addSongList(@RequestBody SongList songList) {
        log.info("管理端添加歌单：{}",songList);
        songList.setPic(PicPathConstant.DEFAULT_SONG_LIST_PIC); // 给歌单设置默认的图片
        boolean flag = songListService.save(songList);
        if (flag) {
            return Result.success(MessageConstant.SONG_LIST_ADD_SUCCESS);
        } else {
            return Result.error(MessageConstant.SONG_LIST_ADD_FAIL);
        }
    }

    /**
     * 管理端更新歌单信息
     * @param songList
     * @return
     */
    @PostMapping("/admin/update")
    public Result updateSongList(@RequestBody SongList songList) {
        log.info("管理端更新歌单信息：{}",songList);
        boolean flag = songListService.updateById(songList);
        if (flag) {
            return Result.success(MessageConstant.ALTER_SUCCESS);
        } else {
            return Result.error(MessageConstant.ALTER_FAIL);
        }
    }

    /**
     * 管理端更新歌单的图片
     * @param imgFile
     * @param id
     * @return
     */
    @PostMapping("/admin/updateImg")
    public Result updateSongListImg(@RequestParam("file") MultipartFile imgFile, @RequestParam("id") Integer id) {
        log.info("管理端更新歌单的图片：{}", id);
        return songListService.updateSongListImg(imgFile, id);
    }

    /**
     * 管理端删除歌单
     * @param id
     * @return
     */
    @GetMapping("/admin/delete")
    public Result deleteSongList(@RequestParam Integer id) {
        log.info("管理端删除歌单：{}",id);
        return songListService.deleteSongList(id);
    }

    /**
     * 查询所有歌单的标题和图片路径
     * @return
     */
    @GetMapping("/common/getSongListPicPath")
    public Result<List<SongListVO>> getSongListPicPath() {
        log.info("查询歌单的标题和图片路径");
        List<SongListVO> songLists = songListService.getSongListPicPath();

        return Result.success(songLists);
    }

    /**
     * 获取所有歌单信息
     * @return
     */
    @GetMapping("/common/getAllSongList")
    public Result<List<SongList>> getAllSongList() {
        log.info("获取所有歌单信息");
        List<SongList> songLists = songListService.list();
        return Result.success(songLists);
    }

    /**
     * 根据歌单风格获取歌单
     * @param style
     * @return
     */
    @GetMapping("/common/style")
    public Result<List<SongList>> getListByStyle(@RequestParam String style) {
        log.info("根据歌单风格获取歌单:{}",style);
        return songListService.getListByStyle(style);
    }

    /**
     * 根据歌单标题模糊查询歌单信息
     * @param title
     * @return
     */
    @GetMapping("/common/likeTitle")
    public Result<List<SongList>> getListLikeTitle(@RequestParam String title) {
        log.info("返回模糊查询歌单标题的歌单：{}",title);
        return songListService.getListLikeTitle(title);
    }

}
